<template>
  <div class="main"
       v-loading="loading"
       element-loading-text="拼命加载中"
       element-loading-spinner="el-icon-loading"
  >

    <el-empty v-if="!loading && !data.length" description="暂无数据"></el-empty>

    <el-tabs tab-position="left">
      <el-tab-pane v-for="(item,i) in data" :key="i" :label="item.attrGroupName">
        <el-form ref="form" label-width="150px">
          <el-form-item v-for="(attr, i) in item.attrs" :key="i" :label="attr.attrName">
            <el-row>
              <el-col :span="12">
                <el-select
                  v-model="attr.value"
                  :multiple="attr.valueType === 1"
                  allow-create
                  default-first-option
                  :placeholder="'请选择或输入 (' + ((attr.valueType === 1) ? '多' : '单') + '选)'"
                  filterable
                  clearable
                  style="width: 100%"
                >
                  <template v-if="attr.valueSelect">
                    <el-option
                      v-for="(item, index) in attr.valueSelect.split(',')"
                      :key="index"
                      :label="item"
                      :value="item"
                    >
                    </el-option>
                  </template>
                </el-select>
              </el-col>
              <el-col :span="12" :push="1">
                <el-checkbox v-model="attr.showDesc" :true-label="1" :false-label="0">快速展示</el-checkbox>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  name: 'pubSpec',
  data() {
    return {
      data: [],
      remember: undefined,
      loading: false
    }
  },
  methods: {
    init(catId, spuId) {
      if (catId) {
        // 页面缓存，只有 catId 变了才发送请求
        if (!this.remember || this.remember !== catId) {
          this.loading = true
          this.data = []
          this.$api.get('/product/attr/specByCatId', { catId: catId, spuId: spuId }).then(({ data }) => {
            data.forEach(item => {
              if(item.attrs) {
                item.attrs.forEach(attr => {
                  if(attr.valueType === 1) {
                    if(attr.value) {
                      attr.value = attr.value.split(',')
                    } else {
                      attr.value = []
                    }
                  }
                })
              }
            })
            this.data = data
            this.remember = catId
            this.loading = false
          })
        }
      } else {
        this.data = []
        this.remember = undefined
      }
    },
    // 获取要提交的值，返回选中的结果
    getForm() {
      const values = []
      let index = 0
      this.data.forEach((item) => {
        if (item.attrs) {
          item.attrs.forEach((attr) => {
            let value = undefined
            if (attr.valueType === 1) {
              // 多选
              if (attr.value && attr.value.length) {
                // 将数组转为字符串，用,分隔
                value = attr.value.join(',')
              }
            } else {
              // 单选
              value = attr.value
            }

            if (value) {
              values[index++] = {
                attrId: attr.attrId,
                value: value,
                showDesc: attr.showDesc
              }
            }
          })
        }
      })
      return values
    },
    // 表单校验
    validate() {
      return this.getForm().length
    }
  }
}
</script>

<style scoped>
.main {
  margin: 30px 70px 0px 70px;
}
</style>
